var prefix = jsctx + "/printerRecord";


var printerRecord = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        },10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable({
                method: 'get',
                url: prefix + "/listPagePrinterRecord",
                iconSize: 'outline',
                striped: true,
                dataType: "json",
                pagination: true,
                singleSelect: false,
                pageSize: 20,
                pageList: [20],
                pageNumber: 1,
                seatch:true,
                sidePagination: "server",
                queryParams: function (params) {
                    return {
                        limit: params.limit,
                        offset: (params.offset / params.limit) + 1,
                        orderNum: $("#priNo").val().trim(),
                        priNo: $("#priNo").val().trim(),
                        stationName: $("#stationName").val().trim(),
                        carNum: $("#carNum").val().trim()
                    };
                },
                // toolbar : '#exampleToolbar',
                columns: [
                    {
                        field: 'number',
                        title: '序号',
                        align: 'center',
                        formatter: function (value, row, index) {
                            return index + 1;
                        }
                    },
                    {field: 'stationName', title: '桩群名称', align: 'left'},
                    {field: 'priNo', title: '打印编号', align: 'left'},
                    {field: 'printtime', title: '打印时间', align: 'left'},
                    {field: 'orderNum', title: '订单号', align: 'left'},
                    {field: 'carnum', title: '车牌号', align: 'left'},
                    {field: 'printcount', title: '打印次数', align: 'left'},
                    {
                        title: '操作',
                        field: 'id',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var e = '<a class="btn btn-primary btn-sm ' + s_reSet_h + '" href="#" title="重置" onclick="printerRecord.reSet(\''
                                + row.id + '\',' + '\'' + row.printCount
                                + '\')"><i class="fa fa-edit">重置</i></a> ';
                            return e;
                        }
                    }]
            });
    };

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
        load();
    };

    /**
     * 增加
     */
    function add() {
        location.href = prefix + "/add";
    };

    /**
     * 修改
     * @param id
     */
    function edit(id) {
        location.href = prefix + "/edit/" + id;
    };

    /**
     * 重置
     * @param id
     */
    function reSet(id, printCount) {
        if (printCount != '0') {
            layer.confirm('确定要重置打印记录？', {
                btn: ['确定', '取消']
            }, function (index) {
                $.ajax({
                    url: prefix + "/reSet",
                    type: "post",
                    data: {
                        'id': id
                    },
                    success: function (data) {
                        layer.close(index);
                        if (data.status == 200) {
                            layer.msg("重置成功");
                            reLoad();
                        } else {
                            layer.msg(data.message);
                        }
                    }
                });
            })
        }
    };

    function exportFile(params) {
        var orderNum = $("#priNo").val().trim();
        var priNo = $("#priNo").val().trim();
        var stationName = $("#stationName").val().trim();
        var carNum = $("#carNum").val().trim();
        var printSize = $("#printSize").val().trim();
        if (printSize == null || printSize == '') {
            printSize = 0;
        }
        location.href = prefix + "/exportPrinter?orderNum=" + orderNum + "&priNo=" + priNo +
            "&stationName=" + stationName + "&carNum=" + carNum + "&printSize=" + printSize;

    };

    /**
     * 返回列表
     */
    function goBack() {
        location.href = prefix + "/index";
    };

    function print() {
        layer.open({
            type: 2,
            title: '打印机凭空打印订单',
            shadeClose: false,
            area: ['600px', '800px'],
            content: prefix + '/printOrder',
            end: function () {
                reLoad();
            }

        });
    };

    function getPriRtua() {
        var id = $("#id").val();
        $.ajax({
            url: prefix + "/selectstationNamePrirtuaById",
            type: "POST",
            data: {
                id: id
            },
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    $("#priRtua").val(data.data.priRtua);
                    $("#stationName").val(data.data.stationName);
                } else {
                    parent.layer.msg(data.message);
                    $("#priRtua").val("");
                    $("#stationName").val("");
                }
            }
        });
    };

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        add: add,
        edit: edit,
        reSet: reSet,
        exportFile: exportFile,
        print: print,
        getPriRtua: getPriRtua
    };
})();

var confirmPrintOrder = (function () {
    function init() {
        validateRule();
    };


    function save() {
        $.ajax({
            cache: true,
            type: "POST",
            dataType: "json",
            async: true,
            url:  prefix + "/savePrintOrder",
            data: {
                priRtua: $("#priRtua").val().trim(),
                stationName: $("#stationName").val().trim(),
                orderNum: $("#orderNum").val().trim(),
                epNo: $("#epNo").val().trim(),
                power: $("#power").val().trim(),
                chargeAmount: $("#chargeAmount").val().trim(),
                startTime: $("#startTime").val().trim(),
                endTime: $("#endTime").val().trim(),
                soc: $("#soc").val().trim(),
                carNum: $("#carNum").val().trim()
            },
            error: function (request) {
                parent.layer.msg("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("打印成功");
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                } else {
                    parent.layer.msg(data.message);
                }
            }
        });
    };

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmEdit").validate({
            rules: {
                // priRtua:{
                //     required: true
                // },
                chargeAmount: {
                    required: true
                },
                power: {
                    required: true
                },
                epNo: {
                    required: true
                },
                carNum: {
                    required: true
                },
                soc: {
                    required: true
                },
                startTime:{
                    required: true
                },
                endTime:{
                    required: true
                }
            },
            messages: {
                // priRtua:{
                //     required: icon + "当前桩群缺少打印机"
                // },
                chargeAmount: {
                    required: icon + "请输入本次充电金额"
                },
                power: {
                    required: icon + "请输入本次充电电量"
                },
                carNum: {
                    required: icon + "请输入车牌号"
                },
                soc: {
                    required: icon + "请输入SOC"
                },
                startTime:{
                    required: icon + "请选择开始时间"
                },
                endTime:{
                    required: icon + "请选择结束时间"
                },
                epNo: {
                    required: icon + "请输入充电桩号"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    };
    return {
        init: init
    };
})();
